package com.yyq.adms.bzgf.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.yyq.adms.bzgf.bpo.StandardPublishBpo;
import com.yyq.adms.bzgf.pojo.StandardPublish;
import com.yyq.adms.yyb.utils.YYBUtils;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;
import com.yyq.sos.util.page.PageFormData;

@Controller("standardPublishAction")
@Scope("prototype")
public class StandardPublishAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private StandardPublish po=new StandardPublish();
	private StandardPublishBpo bpo;
	
	
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
	public StandardPublish getPo() {
		return po;
	}

	public void setPo(StandardPublish po) {
		this.po = po;
	}

	public StandardPublishBpo getBpo() {
		return bpo;
	}

	@Resource(name="standardPublishBpo")
	public void setBpo(StandardPublishBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}


	@Override
	public String execute() throws Exception {
		super.setObj(po);
		super.setBpo(bpo);
		String ret = super.execute();
		po = (StandardPublish)getObj();
		return ret;
	}
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from StandardPublish order by belongYear desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")  order by obj.belongYear desc";
		}
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		return ActionType.ACTION_DATA_LIST;
	}
	
	@Override
	public String add() {
		String hql = "from "+po.getClass().getName() +" where standardPublishName='"+po.getStandardPublishName()+"'";
		if(po.getSourceID() != null && po.getSourceID() != 0){
			hql += " or sourceID="+po.getSourceID();
		}
		if(bpo.count(hql)>0){
			this.setMsg("{processId:\"-1\",msg:\"名称已重复\"}");
			return ActionType.ACTION_MSG;
		}
		return super.add();
	}
	
	@Override
	public String search() {
		String searchProperty = ((String[])this.getFormValue("searchProperty"))[0];
		String searchKey = ((String[])this.getFormValue("searchKey"))[0];
		String orderStr = "";
		if(bpo.getOrderByDescStr() != null){
			orderStr = bpo.getOrderByDescStr();
		}
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			if("sentDep".equals(searchProperty)){
				hql = "select distinct obj from "+po.getClass().getName()+" obj,RecordSentDep rsd,SysDepts d where " +
					  " obj.id=rsd.sourceRecordId and rsd.sentDepId=d.id and rsd.yyqTableName='standardpublish' and d.deptCname like '%"+searchKey+"%' "+orderStr;
			}else if("state".equals(searchProperty)){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String nowDay = sdf.format(new Date());
				String whereSql = "";
				if("1".equals(searchKey)){
					whereSql = " where (obj.publishDate<='"+nowDay+"' or obj.publishDate is null) and obj.dealDate>'"+nowDay+"' and (obj.stopDate>'"+nowDay+"' or obj.stopDate is null)";
				}else if("2".equals(searchKey)){
					whereSql = " where obj.dealDate<='"+nowDay+"' and (obj.stopDate>'"+nowDay+"' or obj.stopDate is null) ";
				}else{
					whereSql = " where obj.stopDate<='"+nowDay+"' ";
				}
				hql = "from "+po.getClass().getName()+" obj "+whereSql+orderStr;
			}else{
				hql = "from "+po.getClass().getName()+" obj where obj."+searchProperty+" like '%"+searchKey+"%' "+orderStr;
			}
		}else{
			if("state".equals(searchProperty)){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String nowDay = sdf.format(new Date());
				String whereSql = "";
				if("1".equals(searchKey)){
					whereSql = " and (obj.publishDate<='"+nowDay+"' or obj.publishDate is null) and obj.dealDate>'"+nowDay+"' and (obj.stopDate>'"+nowDay+"' or obj.stopDate is null)";
				}else if("2".equals(searchKey)){
					whereSql = " and obj.dealDate<='"+nowDay+"' and (obj.stopDate>'"+nowDay+"' or obj.stopDate is null)";
				}else{
					whereSql = " and obj.stopDate<='"+nowDay+"' ";
				}
				hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId  and rsd.yyqTableName='standardpublish'  and rsd.sentDepId="+user.getOrgId()+"))  "+whereSql+orderStr;
			}else{
				hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId  and rsd.yyqTableName='standardpublish'  and rsd.sentDepId="+user.getOrgId()+"))  and obj."+searchProperty+" like '%"+searchKey+"%' "+orderStr;
			}
		}
		this.setFormData(bpo.searchPage(hql, this.getPageForm().getPageNo(), this.getPageForm().getPageSize()));
		//data = bpo.search(hql);
		return ActionType.ACTION_DATA_LIST;
		//return super.search();
	}
}
